set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
/* *******************************************************************
** DATE :
** Writer :
** Memo :
******************************************************************** */
CREATE PROCEDURE [dbo].[TSP_Store_GetSearch]
(
	@IsTransaction	tinyint			= 0
,	@ErrorCode		int				= 0		OUTPUT
,	@ErrorMessage	nvarchar( 1000 )= ''	OUTPUT

,	@strCityName	nvarchar( 10 ) = null
,	@strStoreName	nvarchar( 10 ) = null

,	@PageNo	int	= 1	
,	@PageSize	tinyint	= 10	
,	@CntRow	int	= NULL	OUTPUT
,	@CntTotal	int	= NULL	OUTPUT
)
as
Set NOCOUNT ON

-- Frk Default Value
DECLARE @FrkDateNow DateTime
DECLARE @FrkRowCount int
DECLARE @FrkTranCount int
DECLARE @FrkErrorCode int
DECLARE @FrkErrorMessage nvarchar( 1000 )
Set @FrkDateNow = GetDate()
set @FrkTranCount = @@TRANCOUNT
-- Frk Check


If ( @IsTransaction = 0 )
Begin
	If ( @FrkTranCount < 1 )
	Begin
		Set @FrkErrorCode = -1
		Set @FrkErrorMessage = 'Transaction.'
		goto ERROR
	End
	Else If ( @FrkTranCount > 1 )
	Begin
		Set @FrkErrorCode = -2
		Set @FrkErrorMessage = 'Transaction.'
		goto ERROR
	End
End
Else
Begin
	If ( @FrkTranCount <> 0 )
	Begin
		Set @FrkErrorCode = -3
		Set @FrkErrorMessage = 'Transaction.'
		goto ERROR
	End
	Begin Tran
End

-- Implementation

DECLARE @SkipPage INT
SET @SkipPage = ( ( @PageNo - 1 ) * @PageSize )

IF ( @strCityName IS NOT NULL )
BEGIN
	SELECT
		@CntTotal = Count( * )
	FROM
		dbo.TB_Store with ( nolock )
	WHERE
		IsActive = 'Y'
	AND	strCityName = @strCityName
	AND	1 = CASE WHEN @strStoreName IS NULL OR strStoreName LIKE @strStoreName + '%' THEN 1 ELSE 0 END

	IF ( @PageNo = 1 )
	BEGIN
		SELECT	Top( @PageSize )
			n4StoreNo
		,	strUserID
		,	IsActive
		,	strStoreName
		,	strCompanyNo
		,	strPhone
		,	strPost
		,	strAddress
		,	strCityName
		,	IsTakeout
		,	strTakeout
		,	IsParking
		,	strParking
		,	strStoreSize
		,	strOpenDate
		,	strOpenTime
		,	strPointX
		,	strPointY
		,	strDescription
		,	dtCreateDate
		,	strManagerName
		FROM
			dbo.TB_Store with ( nolock )
		WHERE
			IsActive = 'Y'
		AND	strCityName = @strCityName
		AND	1 = CASE WHEN @strStoreName IS NULL OR strStoreName LIKE @strStoreName + '%' THEN 1 ELSE 0 END
		ORDER BY
			IsActive DESC, strCityName DESC, n4StoreNo DESC
	END
	ELSE
	BEGIN
		SELECT	Top( @PageSize )
			n4StoreNo
		,	strUserID
		,	IsActive
		,	strStoreName
		,	strCompanyNo
		,	strPhone
		,	strPost
		,	strAddress
		,	strCityName
		,	IsTakeout
		,	strTakeout
		,	IsParking
		,	strParking
		,	strStoreSize
		,	strOpenDate
		,	strOpenTime
		,	strPointX
		,	strPointY
		,	strDescription
		,	dtCreateDate
		,	strManagerName
		FROM
		(
			SELECT
				n4StoreNo
			,	strUserID
			,	IsActive
			,	strStoreName
			,	strCompanyNo
			,	strPhone
			,	strPost
			,	strAddress
			,	strCityName
			,	IsTakeout
			,	strTakeout
			,	IsParking
			,	strParking
			,	strStoreSize
			,	strOpenDate
			,	strOpenTime
			,	strPointX
			,	strPointY
			,	strDescription
			,	dtCreateDate
			,	strManagerName
			,	ROW_NUMBER() OVER ( ORDER BY
				IsActive DESC, strCityName DESC, n4StoreNo DESC
			) AS ROWNUM
			FROM
				dbo.TB_Store with ( nolock )
			WHERE
				IsActive = 'Y'
			AND	strCityName = @strCityName
			AND	1 = CASE WHEN @strStoreName IS NULL OR strStoreName LIKE @strStoreName + '%' THEN 1 ELSE 0 END
		)	A
		WHERE
			ROWNUM > @SkipPage
		AND	ROWNUM <= @SkipPage + @PageSize
		
	END
END
ELSE IF ( @strStoreName IS NOT NULL )
BEGIN
	SELECT
		@CntTotal = Count( * )
	FROM
		dbo.TB_Store with ( nolock )
	WHERE
		IsActive = 'Y'
	AND	strStoreName LIKE @strStoreName + '%'

	IF ( @PageNo = 1 )
	BEGIN
		SELECT	Top( @PageSize )
			n4StoreNo
		,	strUserID
		,	IsActive
		,	strStoreName
		,	strCompanyNo
		,	strPhone
		,	strPost
		,	strAddress
		,	strCityName
		,	IsTakeout
		,	strTakeout
		,	IsParking
		,	strParking
		,	strStoreSize
		,	strOpenDate
		,	strOpenTime
		,	strPointX
		,	strPointY
		,	strDescription
		,	dtCreateDate
		,	strManagerName
		FROM
			dbo.TB_Store with ( nolock )
		WHERE
			IsActive = 'Y'
		AND	strStoreName LIKE @strStoreName + '%'
		ORDER BY
			IsActive DESC, strStoreName DESC, n4StoreNo DESC
	END
	ELSE
	BEGIN
		SELECT	Top( @PageSize )
			n4StoreNo
		,	strUserID
		,	IsActive
		,	strStoreName
		,	strCompanyNo
		,	strPhone
		,	strPost
		,	strAddress
		,	strCityName
		,	IsTakeout
		,	strTakeout
		,	IsParking
		,	strParking
		,	strStoreSize
		,	strOpenDate
		,	strOpenTime
		,	strPointX
		,	strPointY
		,	strDescription
		,	dtCreateDate
		,	strManagerName
		FROM
		(
			SELECT
				n4StoreNo
			,	strUserID
			,	IsActive
			,	strStoreName
			,	strCompanyNo
			,	strPhone
			,	strPost
			,	strAddress
			,	strCityName
			,	IsTakeout
			,	strTakeout
			,	IsParking
			,	strParking
			,	strStoreSize
			,	strOpenDate
			,	strOpenTime
			,	strPointX
			,	strPointY
			,	strDescription
			,	dtCreateDate
			,	strManagerName
			,	ROW_NUMBER() OVER ( ORDER BY
				IsActive DESC, strStoreName DESC, n4StoreNo DESC
			) AS ROWNUM
			FROM
				dbo.TB_Store with ( nolock )
			WHERE
				IsActive = 'Y'
			AND	strStoreName LIKE @strStoreName + '%'
		)	A
		WHERE
			ROWNUM > @SkipPage
		AND	ROWNUM <= @SkipPage + @PageSize
		
	END
END
ELSE	
BEGIN
	SELECT
		@CntTotal = Count( * )
	FROM
		dbo.TB_Store with ( nolock )
	WHERE
		IsActive = 'Y'

	IF ( @PageNo = 1 )
	BEGIN
		SELECT	Top( @PageSize )
			n4StoreNo
		,	strUserID
		,	IsActive
		,	strStoreName
		,	strCompanyNo
		,	strPhone
		,	strPost
		,	strAddress
		,	strCityName
		,	IsTakeout
		,	strTakeout
		,	IsParking
		,	strParking
		,	strStoreSize
		,	strOpenDate
		,	strOpenTime
		,	strPointX
		,	strPointY
		,	strDescription
		,	dtCreateDate
		,	strManagerName
		FROM
			dbo.TB_Store with ( nolock )
		WHERE
			IsActive = 'Y'
		ORDER BY
			IsActive DESC, n4StoreNo DESC
	END
	ELSE
	BEGIN
		SELECT	Top( @PageSize )
			n4StoreNo
		,	strUserID
		,	IsActive
		,	strStoreName
		,	strCompanyNo
		,	strPhone
		,	strPost
		,	strAddress
		,	strCityName
		,	IsTakeout
		,	strTakeout
		,	IsParking
		,	strParking
		,	strStoreSize
		,	strOpenDate
		,	strOpenTime
		,	strPointX
		,	strPointY
		,	strDescription
		,	dtCreateDate
		,	strManagerName
		FROM
		(
			SELECT
				n4StoreNo
			,	strUserID
			,	IsActive
			,	strStoreName
			,	strCompanyNo
			,	strPhone
			,	strPost
			,	strAddress
			,	strCityName
			,	IsTakeout
			,	strTakeout
			,	IsParking
			,	strParking
			,	strStoreSize
			,	strOpenDate
			,	strOpenTime
			,	strPointX
			,	strPointY
			,	strDescription
			,	dtCreateDate
			,	strManagerName
			,	ROW_NUMBER() OVER ( ORDER BY
				IsActive DESC, n4StoreNo DESC
			) AS ROWNUM
			FROM
				dbo.TB_Store with ( nolock )
			WHERE
				IsActive = 'Y'
		)	A
		WHERE
			ROWNUM > @SkipPage
		AND	ROWNUM <= @SkipPage + @PageSize
		
	END
END


-- Frk Error Check
SELECT
	@FrkErrorCode = @@ERROR
,	@FrkRowCount = @@RowCount
SET	@CntRow = @FrkRowCount

-- End

COMPLETE:
	If ( @IsTransaction <> 0 )
	Begin
		If ( @@TRANCOUNT = 1 )
		Begin
			Commit Tran
		End
	End
	SET @ErrorCode = 0
	SET @ErrorMessage = ''
	return 0
ERROR:
	If ( @IsTransaction <> 0 )
	Begin
		If ( @@TRANCOUNT = 1 )
		Begin
			Rollback Tran
		End
	End
	SET @ErrorCode = @FrkErrorCode
	SET @ErrorMessage = @FrkErrorMessage
	return @FrkErrorCode
